package com.hospitalsystem.service.impl;

import com.hospitalsystem.dao.DrugDao;
import com.hospitalsystem.entity.Drug;
import com.hospitalsystem.service.DrugService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * (Drug)表服务实现类
 *
 * @author makejava
 * @since 2024-12-28 19:32:38
 */
@Service("drugService")
public class DrugServiceImpl implements DrugService {
    @Resource
    private DrugDao drugDao;

    /**
     * 通过ID查询单条数据
     *
     * @param  drugId
     * @return 实例对象
     */
    @Override
    public Drug queryById(String drugId) {
        return this.drugDao.queryById(drugId);
    }

    /**
     * 分页查询
     *
     * @param drug 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Drug> queryByPage(Drug drug, PageRequest pageRequest) {
        long total = this.drugDao.count(drug);
        return new PageImpl<>(this.drugDao.queryAllByLimit(drug, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param drug 实例对象
     * @return 实例对象
     */
    @Override
    public Drug insert(Drug drug) {
        this.drugDao.insert(drug);
        return drug;
    }

    /**
     * 修改数据
     *
     * @param drug 实例对象
     * @return 实例对象
     */
    @Override
    public Drug update(Drug drug) {
        this.drugDao.update(drug);
        return this.queryById(drug.getDrugId());
    }

    /**
     * 通过主键删除数据
     *
     * @param  drugId
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String drugId) {
        return this.drugDao.deleteById(drugId) > 0;
    }
}
